package com.etc.controller;

import com.alibaba.fastjson.JSON;
import com.etc.entity.Users;
import com.etc.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * zj 用户控制器
 */
@CrossOrigin
@Controller
@Api(tags = "用户管理相关接口")
@RequestMapping("User")
public class UserController {
    @Autowired
    UserService service;
    @GetMapping("/frontLogin")
    @ApiOperation("前台用户登陆")
    @ResponseBody
    public Map<String,Object> getlogin(@RequestParam("username") String username, @RequestParam("password") String userpwd){
        Map<String, Object> map = new HashMap<>();
        Users users = service.getlogin(username, userpwd);
        if (users!=null&&users.getuLevel()==0) {
            map.put("user", users);
            map.put("msg","success");
        }else {
            map.put("msg","fail");
            map.put("user",null);
        }
        return map;
    }

    @GetMapping("/backLogin")
    @ApiOperation("后台用户登陆")
    @ResponseBody
    public Map<String,Object> backLogin(@RequestParam("username") String username, @RequestParam("password") String userpwd){
        Map<String, Object> map = new HashMap<>();
        Users users = service.getlogin(username, userpwd);
        if (users!=null&&users.getuLevel()==1) {
            map.put("user", users);
            map.put("msg","success");
        }else {
            map.put("msg","fail");
            map.put("user",null);
        }
        return map;
    }
    //修改个人信息
    @RequestMapping("/getModUsers")
    @ResponseBody
    public  boolean  getModUsers(@RequestParam(value = "u_id")String u_id,
                                 @RequestParam(value = "u_name")String u_name,
                                 @RequestParam(value = "u_password")String u_password,
                                 @RequestParam(value = "u_sex")String u_sex){
        //调用业务层
        boolean  fian = service.getModUsers(u_id,u_name,u_password,u_sex);
        return  fian;
    }
    @ResponseBody
    @PostMapping("用户注册")
    public Map<String,Object> getAddUsers(@RequestParam String username,
                                          @RequestParam String userpwd,
                                          @RequestParam String uTle,@RequestParam String usex,
                                          @RequestParam(defaultValue = "1.jpg") String uimg){
        Map<String, Object> map = new HashMap<String,Object>();
        Users users = new Users(username, userpwd, uTle, usex, uimg);
        boolean b = service.insert(users);
        map.put("msg",b);
        map.put("users",users);
        return map;
    }
    @GetMapping("/like")
    @ApiOperation("模糊分页")
    @ResponseBody
    public PageInfo<Users> getlike(@RequestParam String username, @RequestParam Integer num,
                                   @RequestParam Integer size){
        PageInfo<Users> info = service.getPage(username, num, size);
        return info;
    }

    @PostMapping("/regist")
    @ApiOperation("用户注册")
    @ResponseBody
    public Map<String,Object> regist(@RequestParam(value="user")String users,
                                     @RequestParam(value="file")MultipartFile file){
        Map<String, Object> map = new HashMap<>();
        Users user = JSON.parseObject(users, Users.class);
        System.out.println(user);
        System.out.println(file);
        String name = file.getOriginalFilename();
        System.out.println(name);
        user.setuImg(name);
        //System.out.println(user);
        //3.定义文件存储位置
        File files = new File("E:\\uploadFile\\" + name);
        //4.写入
        try {
            file.transferTo(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean b = service.regist(user);
        if (b) {
            map.put("msg","success");
        }else {
            map.put("msg","fail");
        }
        return map;
    }
}
